﻿global using Microsoft.AspNetCore.Html;

namespace WXWorkSvc.Common.TagHelpers;
[HtmlTargetElement("desk-input-item", TagStructure = TagStructure.WithoutEndTag)]
public class _DeskInputItemTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    private readonly IHtmlGenerator _HtmlGenerator;

    public _DeskInputItemTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }

    [HtmlAttributeName("asp-for")]
    public ModelExpression For { get; set; } = default!;
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-hidden-")]
    public Dictionary<string, ModelExpression> Hiddens { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-readonly-")]
    public Dictionary<string, ModelExpression> ReadOnlys { get; set; } = new();
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {


        var tbValidationSummary = _HtmlGenerator.GenerateValidationSummary(ViewContext);
        if (tbValidationSummary != null)
            output.Content.AppendHtml(tbValidationSummary);
        List<string> hiddenNames = new List<string>();
        foreach (var f in Hiddens.Values)
        {
            output.Content.AppendHtml(_HtmlGenerator.GenerateHidden(ViewContext, f.ModelExplorer, f.Name, f.Model, false, null));
            hiddenNames.Add(f.Metadata.Name!);
        }
        List<string> readonlyNames = ReadOnlys.Values.Select(ii => ii.Metadata.Name!).ToList();


        foreach (var p in For.Metadata.Properties)
        {
            if (hiddenNames.Contains(p.Name!))
                continue;
            var f = new ModelExpression(For.Name + "." + p.Name, For.ModelExplorer.GetExplorerForProperty(p.Name));


            var labelTag = _HtmlGenerator.GenerateLabel(ViewContext, f.ModelExplorer, f.Name, f.GetDisplayName(), new { @class = "col-sm-2 col-form-label" });
            labelTag.InnerHtml.SetContent(f.GetDisplayName());
            if (!string.IsNullOrEmpty(f.Metadata.Description))
                labelTag.MergeAttribute("title", f.Metadata.Description);

            TagBuilder inputTag;
            labelTag.Attributes.Add("data-valmsg-for", f.Name);
            labelTag.Attributes.Add("data-valmsg-replace", "false");

            f.GetHtmlAttributes(out var htmlAttributes, out var format);
            inputTag = _HtmlGenerator.GenerateTextBox(ViewContext, f.ModelExplorer, f.Name, f.Model, format, htmlAttributes);


            inputTag.Attributes.Add("class", "form-control");
            if (readonlyNames.Contains(p.Name!))
                inputTag.Attributes.Add("readonly", "readonly");
            var rowDivTag = new TagBuilder("div");
            rowDivTag.AddCssClass("mb-3 row");

            var inputDivTag = new TagBuilder("div");
            inputDivTag.AddCssClass("col-sm-10");
            inputDivTag.InnerHtml.SetHtmlContent(inputTag);

            rowDivTag.InnerHtml.SetHtmlContent(labelTag);
            rowDivTag.InnerHtml.AppendHtml(inputDivTag);

            output.Content.AppendHtml(rowDivTag);

        }
        output.TagName = "div";
        output.Attributes.Add("name", "自动创建");

    }
}

